<!-- Horizontal Form -->
 <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">添加菜单</h3>
      </div>
      <!-- /.box-header -->
      <!-- form start -->
      <form class="form-horizontal">
        <div class="box-body">
          <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">类型</label>
            <div class="col-sm-10 typeRadio">
              	<label class="radio-inline">
				<input type="radio" name="typeId" value="1" checked> 菜单 </label>
				<label class="radio-inline">
				<input type="radio" name="typeId" value="2"> 按钮 </label>
            </div>
          </div>
          <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">菜单名称</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="nameId" placeholder="名称">
            </div>
          </div>
          <div class="form-group">
            <label for="parentId" class="col-sm-2 control-label">上级菜单</label>
            <div class="col-sm-10">
              <input type="text" class="form-control load-sys-menu" readonly="readonly" id="parentId" placeholder="上级菜单">
            </div>
          </div>
          <div class="form-group">
            <label for="urlId" class="col-sm-2 control-label">菜单URL</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="urlId" placeholder="url">
            </div>
          </div>
          <div class="form-group">
			<label for="permissionId" class="col-sm-2 control-label">授权标识：</label>
			<div class="col-sm-10">
				<input type="text" id="permissionId"
				       placeholder="多个用逗号分隔，如：user:list,user:create"
					   class="form-control">
			</div>
		  </div>
		  <div class="form-group">
				<label for="sortId" class="col-sm-2 control-label">排序号：</label>
				<div class="col-sm-10">
					<input type="text" id="sortId" placeholder="排序号"
						class="form-control">
				</div>
		 </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
          <button type="button" class="btn btn-default btn-cancel">Cancel</button>
          <button type="button" class="btn btn-info pull-right btn-save">Save</button>
        </div>
        <!-- /.box-footer -->
      </form>
      <!-- zTree 对应的div -->
      <div class="layui-layer layui-layer-page layui-layer-molv layer-anim" id="menuLayer" type="page" times="2" showtime="0" contype="object"
		style="z-index:59891016; width: 300px; height: 450px; top: 100px; left: 500px; display:none">
		<div class="layui-layer-title" style="cursor: move;">选择菜单</div>
		<div class="layui-layer-content" style="height: 358px;">
			<div style="padding: 10px;" class="layui-layer-wrap">
				<ul id="menuTree" class="ztree"></ul>    <!-- 动态加载树 -->
			</div>
		</div>
		<span class="layui-layer-setwin"> <a class="layui-layer-ico layui-layer-close layui-layer-close1 btn-cancel" ></a></span>
		<div class="layui-layer-btn layui-layer-btn-">
			<a class="layui-layer-btn0 btn-confirm">确定</a>
			<a class="layui-layer-btn1 btn-cancel">取消</a>
	     </div>
      </div>
      
  </div>
  <script type="text/javascript" src="bower_components/ztree/jquery.ztree.all.min.js"></script>
  <script type="text/javascript" src="bower_components/layer/layer.js">
  </script>
 <script type="text/javascript">
  var zTree; //zTree是第三方扩展的一个Jquery插件
  //初始化zTree时会用到
  var setting = {
  	data : {
  		simpleData : {
  			enable : true,
  			idKey : "id",  //节点数据中保存唯一标识的属性名称
  			pIdKey : "parentId",  //节点数据中保存其父节点唯一标识的属性名称
  			rootPId : null  //根节点id
  		}//json 格式javascript对象
  	}
  }//json 格式的javascript对象
 
  $(function(){//页面加载完成之执行此函数
	  $(".form-horizontal")
	  .on("click",".load-sys-menu",doLoadZtreeNodes);
	  
	  //保存和取消按钮事件注册
	  $(".box-footer")
	  .on("click",".btn-cancel",doCancel)
	  .on("click",".btn-save",doSaveOrUpdate)
	  
	  //zTree中的确定和取消事件注册
	  $("#menuLayer")
	  .on("click",".btn-cancel",doHideTree)
	  .on("click",".btn-confirm",doSetSelectNode)
	  
	  //获取绑定的数据
	  var rowData=$("#mainContentId").data("rowData");
	  console.log("edit.rowData",rowData);
	  //假如有数据则执行页面数据的初始化操作
	  if(rowData)doInitEditFormData(rowData);
	  
  });
  //初始化表单数据
  function doInitEditFormData(rowData){
	 $("form input[value='"+rowData.type+"']").prop("checked",true),
	 $("#nameId").val(rowData.name),
	 $("#urlId").val(rowData.url),
	 $("#sortId").val(rowData.sort),
	 $("#permissionId").val(rowData.permission),
	 $("#parentId").data("parentId",rowData.parentId);
	 $("#parentId").val(rowData.parentName);
	 //说明：val()用于获取值,val("value") 表示赋值
  }
  function doCancel(){
	  $("#mainContentId").removeData();
	  var url="menu/doMenuListUI.do";
	  $("#mainContentId").load(url);
  }
  function doSaveOrUpdate(){
	  //1.获取表单数据
	  var params=doGetEditFormData();
	  var rowData=$("#mainContentId")
	              .data("rowData");
	  //假如是修改还需要向服务端传递一个记录id
	  if(rowData)params.id=rowData.id;
	  //2.异步提交表单数据
	  var insertUrl="menu/doSaveObject.do";
	  var updateUrl="menu/doUpdateObject.do";
	  var url=rowData?updateUrl:insertUrl;
	  console.log("edit.params",params);
	  $.post(url,params,function(result){
		  if(result.state==1){
			  alert(result.message);
			  doCancel();
		  }else{
			  alert(result.message);
		  }
	  })
  }
  
  function doGetEditFormData(){
	  var params={
		type:$("form input[name='typeId']:checked").val(),
		name:$("#nameId").val(),
		url:$("#urlId").val(),
		sort:$("#sortId").val(),
		permission:$("#permissionId").val(),
		parentId:$("#parentId").data("parentId")
		}
	  return params;
  }
  //设置选中的节点
  function doSetSelectNode(){
	  //1.获取选中的节点对象
	  var nodes=zTree.getSelectedNodes();
	  if(nodes.length==0)return;
	  var node=nodes[0];
	  var rowData=$("#mainContentId").data("rowData");
	  if(rowData&&node.parentId==rowData.id){
		  alert("父元素不允许为子元素");
		  return;
	  }
	  console.log(node);
	  //2.将对象中内容,填充到表单
	  $("#parentId").data("parentId",node.id);
	  $("#parentId").val(node.name);
	  //3.隐藏树对象
	  doHideTree();
  }

  
  //隐藏zTree对应的div
  function doHideTree(){
	  $("#menuLayer").css("display","none");
  }
  function doLoadZtreeNodes(){
	  //debugger
	  //显示zTree所在的div
	  $("#menuLayer").css("display","block");
	  //异步加载zTree节点信息，然后初始化zTree
	  var url="menu/doFindZtreeMenuNodes.do";
	  $.getJSON(url,function(result){
		  if(result.state==1){
			zTree=$.fn.zTree.init(
					$("#menuTree"),
					setting,
					result.data);
			/* var treeObj = $.fn.zTree.getZTreeObj("tree");
			var nodes = treeObj.getSelectedNodes();
			if (nodes && nodes.length>0) {
				treeObj.removeChildNodes(nodes[0]);
			} */
			
		  }else{
			 alert(result.message);
		  }
	  });
  }
 </script>
    
    
    
    
    
    
    
    
    